package com.gxa.customer.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.common.commons.R;
import com.gxa.common.params.AddHousePurchaseParam;
import com.gxa.common.params.QueryHousePurchaseParam;
import com.gxa.common.pojo.*;
import com.gxa.common.utils.RUtils;
import com.gxa.customer.mapper.*;
import com.gxa.customer.service.HousePurchaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HousePurchaseServiceImpl implements HousePurchaseService {
    @Autowired
    private PartnerMapper partnerMapper;
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private HousePurchaseMapper housePurchaseMapper;
    @Autowired
    private HousePaymentStatusMapper housePaymentStatusMapper;
    @Autowired
    private PartnerCustomerMapper partnerCustomerMapper;
    @Autowired
    private ClientListBaseInformationMapper clientListBaseInformationMapper;
    @Override
    public R addHousePurchase(AddHousePurchaseParam param) {
        //①创建置业信息合同
        HousePurchase housePurchase = new HousePurchase();
        BeanUtils.copyProperties(param, housePurchase);
        housePurchaseMapper.insert(housePurchase);
        long housePurchaseId = housePurchase.getId();
        //②置业合同表id绑定给房产信息表+客户房产数量+1
        List<Integer> houseIds = param.getHouseIds();
        House house;
        for (Integer id : houseIds) {
            house = houseMapper.selectById(id);
            System.out.println(house);
            house.setCustomerId(param.getCustomerId());
            house.setHousePurchaseId(housePurchaseId);
            houseMapper.updateById(house);
            //todo
            //调用客户mapper房产数量+1
            clientListBaseInformationMapper.addPropertiesToClient(param.getCustomerId());
        }
        //③记录合作方id与客户id对应关系(中间表)
        long partnerId = param.getPartnerId();
        long customerId = param.getCustomerId();
        PartnerCustomer partnerCustomer = new PartnerCustomer();
        partnerCustomer.setCustomerId(customerId);
        partnerCustomer.setPartnerId(partnerId);
        partnerCustomerMapper.insert(partnerCustomer);
        //④合作方客户数量+1
        PartnerInfo partnerInfo = partnerMapper.selectById(param.getPartnerId());
        partnerInfo.setCustomerCount(partnerInfo.getCustomerCount() + 1);
        partnerMapper.updateById(partnerInfo);
        //⑤创建一条房款及第三方支付情况
        HousePaymentStatus housePaymentStatus = new HousePaymentStatus();
        housePaymentStatus.setForProjectId(param.getForProjectId());
        housePaymentStatus.setForProjectName(param.getForProjectName());
        housePaymentStatus.setRealHousePrice(param.getRealHousePrice());
        housePaymentStatus.setIsPayment((byte) 0);
        housePaymentStatus.setHouse_purchase_id(housePurchase.getId());
        housePaymentStatus.setCustomerId(param.getCustomerId());
        housePaymentStatus.setNationId(param.getNationId());
        housePaymentStatusMapper.insert(housePaymentStatus);
        return RUtils.success();
    }

    @Override
    public R queryHousePurchase(QueryHousePurchaseParam param) {
        QueryWrapper<HousePurchase> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", param.getCustomerId()).eq("nation_id", param.getNationId());
        List<HousePurchase> list = housePurchaseMapper.selectList(wrapper);
        for (HousePurchase housePurchase : list) {
            //合作方详情信息
            long partnerId = housePurchase.getPartnerId();
            PartnerInfo partnerInfo = partnerMapper.selectById(partnerId);
            housePurchase.setPartnerInfo(partnerInfo);
            //房产详情信息
            long housePurchaseId = housePurchase.getId();
            QueryWrapper<House> houseQueryWrapper = new QueryWrapper<>();
            houseQueryWrapper.eq("house_purchase_id", housePurchaseId);
            List<House> houseList = houseMapper.selectList(houseQueryWrapper);
            housePurchase.setHouseList(houseList);
        }
        return RUtils.success(list);
    }
}
